package com.hatom.cameraxhelp.fragment.dialog;

import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.camera.core.ImageProxy;
import androidx.camera.view.PreviewView;
import androidx.fragment.app.DialogFragment;

import com.blankj.utilcode.util.ToastUtils;
import com.hatom.cameraxhelp.R;
import com.hatom.cameraxhelp.bean.CameraParam;
import com.hatom.cameraxhelp.utils.Base64Utils;
import com.hatom.cameraxhelp.utils.CameraXHelp;
import com.hatom.cameraxhelp.widgets.MultFaceView;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import org.jetbrains.annotations.NotNull;

import java.util.List;


public class FaceMultiChooseDialog extends DialogFragment {

    private PreviewView camera_preview;//预览页面

    private MultFaceView multFaceView;//多人脸View

    private CameraXHelp cameraXHelp;//CrameX工具类


    private CameraParam mCameraParam;


    public FaceMultiChooseDialog(CameraParam cameraParam) {
        this.mCameraParam=cameraParam;
    }

    public FaceMultiChooseDialog() {
        this.mCameraParam=new CameraParam();//初始化
    }

    private GetFace getFace;

    public void setGetFace(GetFace getFace) {
        this.getFace = getFace;
    }

    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
    }


    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.camerax_fragment_dialogmultface_camerax, container, false);
        initView(view);
        return view;
    }

    private void initWindow(){
        Window window = requireDialog().getWindow();

        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Color.TRANSPARENT);
        WindowManager.LayoutParams layoutParams = window.getAttributes();//获取dialog布局的参数
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;//全屏
        layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;//全屏
        //设置导航栏颜
        window.setNavigationBarColor(Color.TRANSPARENT);
        //内容扩展到导航栏
        window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL);
        if (Build.VERSION.SDK_INT>=28){
            layoutParams.layoutInDisplayCutoutMode=WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
        }

        layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        layoutParams.windowAnimations = android.R.anim.fade_in;
        window.requestFeature(Window.FEATURE_NO_TITLE);

        window.setAttributes(layoutParams);
    }

    private void initView(View view) {
        initWindow();
        camera_preview = view.findViewById(R.id.dialogmult_camera_preview);
        AppCompatImageView cameraTrans=view.findViewById(R.id.multi_trans_camera);


        multFaceView = view.findViewById(R.id.multFaceView);

        multFaceView.setFaceNum(mCameraParam.getFaceNum());
        multFaceView.setMaxStayTime(mCameraParam.getMaxStayTime());

        AppCompatImageView close_dialog=view.findViewById(R.id.close_dialog);
        close_dialog.setColorFilter(Color.WHITE);
        close_dialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getFace!=null){
                    getFace.error("close");
                }
                dismiss();
            }
        });//关闭按钮
        cameraTrans.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cameraXHelp!=null){
                    cameraXHelp.transCamera();
                }
            }
        });

        XXPermissions.with(requireActivity())
                .permission(Permission.CAMERA)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all){
                            initCamera();
                        }else {
                            ToastUtils.showShort("相机权限未通过");
                        }
                    }
                });
    }

    //初始化相机
    private void initCamera(){
        cameraXHelp=new CameraXHelp();//初始化工具类对象
        cameraXHelp.setBackCamera(mCameraParam.isBlackCamera());
        multFaceView.setFaceClickListener(new MultFaceView.FaceClickListener() {
            @Override
            public void clickface(Bitmap bitmap) {
                Log.e("TEST","clickface");
                if (getFace!=null){
                    getFace.getPhoto(Base64Utils.bitmapToBase64(bitmap));
                }
                dismiss();
            }

            @Override
            public void clickface(int postion) {
                Log.e("TEST","clickface postion"+postion);
            }
        });
        cameraXHelp.setUpCamera(FaceMultiChooseDialog.this,requireActivity(), camera_preview.getSurfaceProvider(), new CameraXHelp.AnalaysListener() {
            @Override
            public void analyze(@NonNull @NotNull ImageProxy imageProxy) {
                multFaceView.detect(imageProxy);
            }

            @Override
            public void error(String error) {
                if (getFace!=null){
                    getFace.error(error);
                }
            }
        });

    }


    @Override
    public void onDismiss(@NonNull @NotNull DialogInterface dialog) {
        super.onDismiss(dialog);
        if (getFace!=null){
            getFace.error("onDismiss");
        }
    }

    public interface GetFace{
        void getPhoto(String base64);
        void error(String msg);
    }
}
